import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, tween } from 'cc';
import { TriangleItem } from './TriangleItem';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('RotateTriangle')
export class RotateTriangle extends Component {

    @property(Collider2D)
    Collider2Ds: Collider2D[] = [];

    @property
    Timer: number = 15;

    private _isPause: boolean = false;

    protected onLoad(): void {
        this.Collider2Ds.forEach(e => {
            if (e) {
                e.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            }
        })
    }

    protected update(dt: number): void {
        if (this._isPause) return;
        this.node.angle += (360 / this.Timer) * dt;
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.group == 1 << 1) {
            otherCollider.getComponent(TriangleItem).free();
        }
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }
}


